﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Caching;
using System.Web;
using Newtonsoft.Json;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace KlirProject.WP.LoginWP
{
    public partial class LoginWPUserControl : UserControl
    {
        public string RedirectUrl { get; set; }
        public string jsonresponse { get; set; }
       [DataMember(Name = "parameters")]
       public List<Dictionary<string, object>> Parameters { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Klir"]!=null)
            {
                var serializer = new JavaScriptSerializer();
                string jsonObject = Session["Klir"].ToString();
                var deserializedResult = serializer.Deserialize<List<User>>(jsonObject);
                jsonresponse = serializer.Serialize(jsonObject);
            }   
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(username.Text) && !string.IsNullOrEmpty(password.Text))
            {
                try
                {
                    KlirWebServices.KlirWS KlirWS = new KlirWebServices.KlirWS();
                    string results = KlirWS.Login(username.Text, password.Text);
                    if (!string.IsNullOrEmpty(results))
                    {
                        if (results!="")
                        {
                            System.Web.HttpCookie HttpCookie = new System.Web.HttpCookie("KlirSitePermt", results);
                            HttpCookie.Expires.AddHours(3);
                            Response.Cookies.Add(HttpCookie);

                            var UserInfo = KlirWS.GetUserInfo(results);

                            Session["Klir"] = UserInfo;
                            Session.Timeout = 60;
                           // messegeLBL.Text = Session["Klir"].ToString();
                            if (!string.IsNullOrEmpty(RedirectUrl))
                            {
                                Response.Redirect(RedirectUrl + "?permit=1");
                            }
                            else
                            {
                                Response.Redirect(Request.Url.AbsolutePath + "?permit=1");
                            }
                        }
                        else
                        {
                            messegeLBL.Text = "אינך רשום במערכת נסה שנית";
                            
                        }
                    }
                   // messegeLBL.Text = "אינך רשום במערכת נסה שנית";
                }
                catch (Exception ex)
                {
                    messegeLBL.Text = ex.ToString();
                    throw;
                }

            }
        }
    }
}
